package com.tcwlkj.uuService.fragment;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.adapter.HonorAdapter;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.GoodEvaluteModel;
import com.tcwlkj.uuService.model.HonorBean;
import com.tcwlkj.uuService.model.ProfitDataModel;
import com.tcwlkj.uuService.ui.HonorsActivity;
import com.tcwlkj.uuService.utils.MyRefresh;
import com.tcwlkj.uuService.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by ${tanbo} on 2017/9/11 0011.
 */

public class HonorsFragment extends Fragment {
    private View view;
    private RecyclerView rcv;
    public HonorsActivity baseContext;
    GoodEvaluteModel mGoodEvaluteModel;
    ProfitDataModel mProfitDataModel;
    HonorAdapter mHonorAdapter;
    private List<GoodEvaluteModel> modelist = new ArrayList<GoodEvaluteModel>();
    public static final String GOODS_TYPE = "Get_Evaluate";
    public static final String INCOME_TYPE = "Get_Profit";
    private int positionType = 0;
    private TextView one_day_seniority, seven_day_seniority, one_month_seniority;
    private List<TextView> mTextViewList = new ArrayList<TextView>();
    private TwinklingRefreshLayout twinklingRefreshLayout;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_honor, null);
        baseContext = (HonorsActivity) getActivity();
        GetGoodEvaluteData("7",GOODS_TYPE,"7");

        initTextView();
        initHonorView();
        baseContext.setPagerchangedListener(new HonorsActivity.OnPagerChangedListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void OnPagerChangedListener(int position) {
                if (position == 0) {
                    GetGoodEvaluteData("7",GOODS_TYPE,"7");
                    one_day_seniority.setVisibility(View.GONE);
                    changeBackgroud(seven_day_seniority);
                } else if (position == 1) {
                    GetProfitData("1",INCOME_TYPE,"1");
                    one_day_seniority.setVisibility(View.VISIBLE);
                    changeBackgroud(one_day_seniority);
                }
                positionType = position;

            }
        });
        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void initTextView() {
        twinklingRefreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.refreshLayout);
        MyRefresh.getInstance().getMyRefresh(twinklingRefreshLayout,true);
        one_day_seniority = (TextView) view.findViewById(R.id.one_day_seniority);
        one_day_seniority.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                GetProfitData("1",INCOME_TYPE,"1");
                changeBackgroud(one_day_seniority);
            }
        });
        seven_day_seniority = (TextView) view.findViewById(R.id.seven_day_seniority);
        seven_day_seniority.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                changeBackgroud(seven_day_seniority);
                if (positionType == 0) {
                    GetGoodEvaluteData("7",GOODS_TYPE,"7");
                } else if (positionType == 1) {
                    GetProfitData("7",INCOME_TYPE,"7");

                }
            }
        });
        one_month_seniority = (TextView) view.findViewById(R.id.one_month_seniority);
        one_month_seniority.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                changeBackgroud(one_month_seniority);
                if (positionType == 0) {
                    GetGoodEvaluteData("30",GOODS_TYPE,"30");
                } else if (positionType == 1) {
                    GetProfitData("30",INCOME_TYPE,"30");
                }
            }
        });
        mTextViewList.add(one_day_seniority);
        mTextViewList.add(seven_day_seniority);
        mTextViewList.add(one_month_seniority);
        changeBackgroud(seven_day_seniority);
    }

    private void initHonorView() {
        rcv = (RecyclerView) view.findViewById(R.id.rcv_honor);
        rcv.setLayoutManager(new LinearLayoutManager(baseContext));
        rcv.addItemDecoration(new DividerItemDecoration(
                baseContext, DividerItemDecoration.VERTICAL));
        mHonorAdapter = new HonorAdapter(getActivity(), modelist, GOODS_TYPE,"7");
        rcv.setAdapter(mHonorAdapter);
    }

    public void GetGoodEvaluteData(final String days,String type,String time) {
        modelist.clear();
        Call<CommonResponse<List<GoodEvaluteModel>>> httpResultCall = RetrofitUtils.getRequest(getContext(),false).Get_Evaluate(days, "1","4");

        httpResultCall.enqueue(new HttpCallBack<CommonResponse<List<GoodEvaluteModel>>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<List<GoodEvaluteModel>> commonResponse= (CommonResponse<List<GoodEvaluteModel>>) t;
                List<GoodEvaluteModel> goodEvaluteModels=commonResponse.getData();
                mHonorAdapter.updateData(goodEvaluteModels,type,time);
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    GetGoodEvaluteData(days,type,time);
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    public void GetProfitData(final String days, String type,String time) {
        modelist.clear();
        Call<CommonResponse<List<GoodEvaluteModel>>> httpResultCall = RetrofitUtils.getRequest(getContext(),false).Get_Profit(days);

        httpResultCall.enqueue(new HttpCallBack<CommonResponse<List<GoodEvaluteModel>>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<List<GoodEvaluteModel>> commonResponse= (CommonResponse<List<GoodEvaluteModel>>) t;
                List<GoodEvaluteModel> goodEvaluteModels=commonResponse.getData();
                mHonorAdapter.updateData(goodEvaluteModels,type,time);
            }

            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    GetGoodEvaluteData(days,type,time);
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void changeBackgroud(TextView textView) {
        for (TextView text : mTextViewList) {
            if (textView == text) {
                //text.setBackground(getResources().getDrawable(R.color.base_blue1));
                text.setBackgroundResource(R.drawable.text_bule_border);
            } else {
                //text.setBackground(getResources().getDrawable(R.color.text_999));
                text.setBackgroundResource(R.drawable.text_gray_border);

            }
        }
    }
}